<template>
  <div id="BookBorrowing"></div>
</template>

<script>
import * as echarts from "echarts/core";
import { PolarComponent } from "echarts/components";
import { PieChart } from "echarts/charts";
import { LabelLayout } from "echarts/features";
import { CanvasRenderer } from "echarts/renderers";
import pubsub from "pubsub-js";

export default {
  name: "BookBorrowing",
  data() {
    return {
      borrowInfo: [
        {
          value: 770,
          name: "计算机学院",
        },
        {
          value: 570,
          name: "医学院",
        },
        {
          value: 770,
          name: "商学院",
        },
        {
          value: 970,
          name: "电信学院",
        },
        {
          value: 670,
          name: "外语学院",
        },
        {
          value: 265,
          name: "管理学院",
        },
      ],
    };
  },

  mounted() {
    this.initialize();
    
    this.interval = window.setInterval(() => {
      this.initBorrowInfo();
      this.$nextTick(()=>{
        this.initialize();
      })
    }, Math.random() * 1000 + 1500);
  },

  methods: {
    initBorrowInfo() {
      for (let index in this.borrowInfo) {
        this.borrowInfo[index].value +=
          Math.floor(Math.random() * (100 - 1 + 1)) + 1;
          // console.log(this.borrowInfo[index].value);
      }
    },
    initialize() {
      echarts.use([PolarComponent, PieChart, CanvasRenderer, LabelLayout]);

      var chartDom = document.getElementById("BookBorrowing");
      var myChart = echarts.init(chartDom);
      var option;
      option = {
        //底部背景色
        // backgroundColor: '#fff',
        color: [
          "#EAEA26",
          "#906BF9",
          "#FE5656",
          "#01E17E",
          "#3DD1F9",
          "#FFAD05",
        ],
        // grid: {
        //   left: 100,
        //   top: 50,
        //   bottom: 10,
        //   right: 10,
        //   containLabel: true
        // },
        polar: {},
        angleAxis: {
          interval: 1,
          type: "category",
          data: [],
          z: 10,
          axisLine: {
            show: false,
            lineStyle: {
              color: "#0B4A6B",
              width: 1,
              type: "solid",
            },
          },
          axisLabel: {
            show: false,
            interval: 0,

            color: "#0B4A6B",
            margin: 8,
            fontSize: 14,
          },
        },
        radiusAxis: {
          min: 40,
          max: 120,
          interval: 20,
          axisLine: {
            show: false,
          },
          axisLabel: {
            formatter: "{value} %",
            show: false,
            padding: [0, 0, 20, 0],
            color: "#0B3E5E",
            fontSize: 16,
          },
          splitLine: {
            lineStyle: {
              //底部线的颜色
              //#F1F8FF
              color: "#DDEEFF",
              width: 2,
              type: "solid",
            },
          },
        },
        calculable: true,
        series: [
          {
            type: "pie",
            radius: ["17%", "18%"],
            hoverAnimation: false,
            labelLine: {
              normal: {
                show: false,
                length: 30,
                length2: 55,
              },
            },
            data: [
              {
                name: "",
                value: 0,
                itemStyle: {
                  normal: {
                    color: "#B5CCE7",
                  },
                },
                tooltip: {
                  show: false,
                },
              },
            ],
          },
          {
            type: "pie",
            radius: ["90%", "92%"],
            hoverAnimation: false,
            labelLine: {
              normal: {
                show: false,
                length: 30,
                length2: 55,
              },
            },
            name: "",
            data: [
              {
                name: "",
                value: 0,
                itemStyle: {
                  normal: {
                    //底部最外层线的颜色
                    color: "#DDEEFF",
                  },
                },
                tooltip: {
                  show: false,
                },
              },
            ],
          },
          {
            stack: "a",
            type: "pie",
            radius: ["30%", "70%"],
            roseType: "area",
            zlevel: 10,

            label: {
              // shadowColor: 'rgba(0, 0, 0, 0.5)', // 阴影颜色
              normal: {
                show: true,
                formatter: function (param) {
                  // console.log(param);
                  let style = "";
                  // switch (param.name) {
                  //     //
                  // }
                  return (
                    "{a|" +
                    param.name +
                    "}\n\n" +
                    style +
                    // param.value +
                    // '本' +
                    // ' | ' +
                    param.percent +
                    "%"
                  );
                },
                textStyle: {
                  fontSize: 14.5,
                  color: "#ffffff", // 修改文字颜色为白色
                },
                rich: {
                  a: {
                    color: "#fff",
                    fontSize: "16",
                  },
                  s1: {
                    color: "#F25646",
                    fontSize: "16",
                  },
                  s2: {
                    color: "#FA971E",
                    fontSize: "16",
                  },
                  s3: {
                    color: "#EDC644",
                    fontSize: "16",
                  },
                  s4: {
                    color: "#47B17D",
                    fontSize: "16",
                  },
                  s5: {
                    color: "#43C9D2",
                    fontSize: "16",
                  },
                  s6: {
                    color: "yellow",
                    fontSize: "16",
                  },
                },
                position: "outside",
              },
              emphasis: {
                show: true,
              },
            },
            itemStyle: {
              normal: {
                color: function (params) {
                  //图表颜色的渐变效果
                  const colorList = [
                    {
                      c1: "#c5223b",
                      c2: "#fa768a",
                    },
                    {
                      c1: " #de7110",
                      c2: "#fecd70",
                    },
                    {
                      c1: "#d09f08",
                      c2: "#FFD145",
                    },
                    {
                      c1: "#238483",
                      c2: "#55C27C",
                    },
                    {
                      c1: " #45EAFF",
                      c2: "#40ADAC",
                    },
                    {
                      c1: "#12B3F8",
                      c2: "#7DE8FF",
                    },
                    {
                      c1: " #0176D3",
                      c2: "#13B7FF",
                    },
                    {
                      c1: "#015BD3",
                      c2: "#138DFF",
                    },
                    {
                      c1: " #7c94e7",
                      c2: "#1e2783",
                    },
                  ];
                  return new echarts.graphic.LinearGradient(1, 0, 0, 0, [
                    {
                      offset: 0,
                      color: colorList[params.dataIndex].c1,
                    },
                    {
                      offset: 1,
                      color: colorList[params.dataIndex].c2,
                    },
                  ]);
                },
              },
            },
            labelLine: {
              normal: {
                show: true,
                length: 10,
                length2: 30,
                color: "#4BCFFC",
              },
              emphasis: {
                show: true,
              },
            },
            data: this.borrowInfo,
          },
        ],
      };
      option && myChart.setOption(option);

      // 存储 Vue 实例的引用
      const vm = this;

      myChart.on("click", function (params) {
        let collegeName = params.name;
        // console.log(params.name);
        if (collegeName == "电信学院") {
          collegeName = "电信与智能制造学院";
        } else if (collegeName == "计算机学院") {
          collegeName = "计算机与软件工程学院";
        }
        // 路由跳转
        vm.$router.push("/department/level");

        setTimeout(function () {
          pubsub.publish("sendCollegeName", collegeName);
        }, 0);
      });
    },
  },

  beforeDestroy() {
    window.clearInterval(this.interval); // 一定要清除定时器
  },
};
</script>

<style scoped>
#BookBorrowing {
  width: 100%;
  height: 100%;
}
</style>
